import { message } from 'antd';

import request from '@/utils/request';

// 类型定义
interface Role {
  id: string;
  name: string;
  label: string;
  status: number;
  order?: number;
  desc?: string;
  permission?: string[];
}

interface RoleListParams {
  page?: number;
  pageSize?: number;
  keyword?: string;
}

interface RoleListResponse {
  list: Role[];
  total: number;
}

// 获取角色列表
export const getRoleList = async (params?: RoleListParams) => {
  try {
    const response = await request.get<RoleListResponse>('/users', { params });
    return response;
  } catch (error) {
    message.error('获取角色列表失败');
    throw error;
  }
};

// 创建角色
export const createRole = async (data: Omit<Role, 'id'>) => {
  try {
    const response = await request.post<Role>('/roles', data);
    message.success('创建成功');
    return response;
  } catch (error) {
    message.error('创建角色失败');
    throw error;
  }
};

// 更新角色
export const updateRole = async (id: string, data: Partial<Role>) => {
  try {
    const response = await request.put<Role>(`/roles/${id}`, data);
    message.success('更新成功');
    return response;
  } catch (error) {
    message.error('更新角色失败');
    throw error;
  }
};

// 删除角色
export const deleteRole = async (id: string) => {
  try {
    await request.delete(`/roles/${id}`);
    message.success('删除成功');
  } catch (error) {
    message.error('删除角色失败');
    throw error;
  }
};

// 导出类型
export type { Role, RoleListParams, RoleListResponse };
